#ifndef _CRT_CORE_HPP_
#define _CRT_CORE_HPP_

#include <crt_targetos.hpp>

#if 1
#define LIKELY(x)   __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY
#define UNLIKELY
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif

#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifndef __USE_UNIX98
#define __USE_UNIX98
#endif

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <wchar.h>

#if (TARGET_OS == OS_WIN)
#include <winosdef.h>
#include <winossysdef.h>
#include <winsysvar.hpp>
#elif (TARGET_OS == OS_POSIX)
#include <posixosdef.h>
#include <posixossysdef.h>
#include <posixsysvar.hpp>
#elif (TARGET_OS == OS_MACH)
#include <machosdef.h>
#include <machossysdef.h>
#include <machsysvar.hpp>
#elif (TARGET_OS == OS_UNIX)
#include <unixosdef.h>
#include <unixossysdef.h>
#include <unixsysvar.hpp>
#endif

#include <crt_common.hpp>
#include <crt_def.h>
#include <crt_error.h>

typedef unsigned int crtid;
typedef unsigned int crtop;

typedef intptr_t crt_int_t;
typedef uintptr_t crt_uint_t;
typedef intptr_t crt_flag_t;

#define CRT_INT32_LEN (sizeof("-2147483648") - 1)
#define CRT_INT64_LEN (sizeof("-9223372036854775808") - 1)

#if (TARGET_BITS == ARCH_32BIT)
#define CRT_INT_T_LEN       NGX_INT32_LEN
#define CRT_MAX_INT_T_VALUE 2147483647
#else
#define CRT_INT_T_LEN       NGX_INT64_LEN
#define CRT_MAX_INT_T_VALUE 9223372036854775807
#endif

// 2147483647:seconds = 68.0962597:years
typedef long crt_msec;

#define CRT_UNSET      -1
#define CRT_UNSET_UINT (crt_int_t) - 1
#define CRT_UNSET_PTR  (void*)-1
#define CRT_UNSET_SIZE (size_t) - 1
#define CRT_UNSET_MSEC (crt_msec) - 1

#ifndef CRT_ALIGNMENT
#define CRT_ALIGNMENT sizeof(unsigned long) /* platform word */
#endif

#endif
